package UILayer;
import ModelLayer.*;
import java.util.*;
import CtrlLayer.*;

public class CustomersUI
{
    CustomerController customerController;
    public CustomersUI()
    {
        customerController = new CustomerController();
    }

    public void customerMenuStart(int sessionId)
    {
        boolean exit=false;
        while (!exit) 
        {
            int choice = writeCustomersMenu(sessionId);
            if(  SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2) {
                switch(choice){
                    case 1:
                    startCreateCustomer(sessionId);
                    break;
                    case 2:
                    startFindCustomer();
                    break;
                    case 3:
                    startUpdateCustomer(sessionId);
                    break;
                    case 4:
                    startDeleteCustomer();
                    break;
                    case 0:
                    exit = true;
                    break;
                }
            } else {
                switch(choice){
                    case 1:
                    startCreateCustomer(sessionId);
                    break;
                    case 2:
                    startFindCustomer();
                    break;
                    case 3:
                    startUpdateCustomer(sessionId);
                    break;
                    case 0:
                    exit = true;
                    break;
                }
            }
        }
    }

    private int writeCustomersMenu(int sessionId)
    {       

        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Customers Menu ***");
        if(  SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2 ){
            System.out.println(" (1) Add customer");
            System.out.println(" (2) Show customer");
            System.out.println(" (3) Update customer");
            System.out.println(" (4) Delete customer"); 
        }else{
            System.out.println(" (1) Add customer(without discounts)");
            System.out.println(" (2) Show customer");
            System.out.println(" (3) Update customer(without discounts)");
        }

        System.out.println(" (0) Back");
        System.out.print("\n Make your choice: ");
        int choice = keyboard.nextInt();
        return choice;
    }

    private void startCreateCustomer(int sessionId)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the information of the new customer:");
        System.out.println("ID: ");
        int id = Integer.parseInt(keyboard.nextLine());
        System.out.println("Name: ");
        String name = keyboard.nextLine();
        System.out.println("E-mail: ");
        String email = keyboard.nextLine();
        System.out.println("Phone number: ");
        String phoneNumber = keyboard.nextLine();
        System.out.println("Address: ");
        String address = keyboard.nextLine();
        int discount = 0;
        if(  SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2 ){
            System.out.println("Discount: ");
            discount = Integer.parseInt(keyboard.nextLine());
        }
        
        if(customerController.createCustomer(id,name,email,phoneNumber,address,discount)) {
            System.out.println("Customer with ID " + id + " has been added!");
        } else {
            System.out.println("Customer with ID " + id + " already exists!");
        }
        
        pause();
    }

    private void startFindCustomer()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the ID of the customer: ");
        int id = Integer.parseInt(keyboard.nextLine());
        if(customerController.findCustomer(id) != null) {
            Customer c = customerController.findCustomer(id);

            System.out.println("ID: ");
            System.out.println(c.getId());

            System.out.println("Name: ");
            System.out.println(c.getName());

            System.out.println("Email: ");
            System.out.println(c.getEmail());

            System.out.println("Phone number: ");
            System.out.println(c.getPhoneNumber());

            System.out.println("Address: ");
            System.out.println(c.getAddress());

            System.out.println("Discount: ");
            System.out.println(c.getDiscount());

        } else {
            System.out.println("The customer has not been found!");
        }
        pause();
    }

    private void startUpdateCustomer(int sessionId)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the ID of the customer:");
        int id = Integer.parseInt(keyboard.nextLine());
        System.out.println("Name: ");
        String name = keyboard.nextLine();
        System.out.println("E-mail: ");
        String email = keyboard.nextLine();
        System.out.println("Phone number: ");
        String phoneNumber = keyboard.nextLine();
        System.out.println("Address: ");
        String address = keyboard.nextLine();
        int discount = 0;
        if(SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2){
            System.out.println("Discount: ");
            discount = Integer.parseInt(keyboard.nextLine());
        }
        customerController.updateCustomer(id,name,email,phoneNumber,address,discount);
        pause();
    }

    private void startDeleteCustomer()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the ID of the customer:");
        int id = Integer.parseInt(keyboard.nextLine());
        boolean del = customerController.deleteCustomer(id);
        if(del)
            System.out.println("Deleted!");
        else
            System.out.println("There is a problem, check the customer for not returned lease!");
        pause();
    }

    private void pause()
    {
        Scanner keyboard = new Scanner(System.in);
        String vent = keyboard.nextLine();
    }

}
